using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Zsa.Emulation.Cartridge;

namespace Zsa.Emulation.CNes.UI
{
    /// <summary>
    /// Used to list some carts.
    /// </summary>
    public partial class CartListControl : UserControl
    {
        #region Fields
        /// <summary>
        /// The cart selected event.
        /// </summary>
        private event Action<CartridgeFileInfo> _CartSelectedEvent;

        /// <summary>
        /// The cart list.
        /// </summary>
        private List<CartridgeFileInfo> _CartList = new List<CartridgeFileInfo>();

        /// <summary>
        /// The selected cart index from the List<CartInfo> not the list control.
        /// </summary>
        private int _SelectedCartIndex;
        #endregion Fields

        #region Contructor
        /// <summary>
        /// Initializes a new instance of the <see cref="CartListControl"/> class.
        /// </summary>
        public CartListControl()
        {
            InitializeComponent();
        }
        #endregion Contructor

        #region Properties

        /// <summary>
        /// Gets the selected cart.
        /// </summary>
        /// <value>The selected cart.</value>
        public CartridgeFileInfo SelectedCart
        {
            get
            {
                if (_SelectedCartIndex < 1)
                {
                    return null;
                }
                return _CartList[_SelectedCartIndex];
            }
        }

        #endregion Properties

        #region Events
        /// <summary>
        /// The cart selected event.
        /// TODO : Change to use a normal event handler.
        /// </summary>
        public event Action<CartridgeFileInfo> CartSelected
        {
            add
            {
                _CartSelectedEvent += value;
            }
            remove
            {
                _CartSelectedEvent -= value;
            }
        }

        #endregion Events


        #region Methods
        /// <summary>
        /// Sets the cart list.
        /// </summary>
        /// <param name="cartList">The cart list.</param>
        public void SetCartList(IList<CartridgeFileInfo> cartList)
        {
            _CartList.Clear();
            if (cartList != null)
            {
                _CartList.AddRange(cartList);
                //_CartList.RemoveAll(
                //    delegate(CartridgeFileInfo cartInfo)
                //    {
                //        return cartInfo.IsEmpty;
                //    }
                //);
                RefreshList();
            }
        }

        /// <summary>
        /// Updates the list.
        /// </summary>
        private void RefreshList()
        {
            listViewCarts.Items.Clear();
            ListViewItem _ListViewItem;
            CartridgeFileInfo _CartInfoTmp;

            for (int _CartInfoIndex = 0; _CartInfoIndex < _CartList.Count; _CartInfoIndex++)
            {
                _CartInfoTmp = _CartList[_CartInfoIndex];
                _ListViewItem = new ListViewItem(_CartInfoTmp.FileName);
                _ListViewItem.SubItems.Add(_CartInfoTmp.MapperInformation.ToString());
                listViewCarts.Items.Add(_ListViewItem);
            }
        }



        /// <summary>
        /// Handles the SelectedIndexChanged event of the listViewCarts control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void listViewCarts_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listViewCarts.SelectedIndices.Count < 1)
            {
                _CartSelectedEvent(null);
            }
            else
            {
                _CartSelectedEvent(
                    _CartList[listViewCarts.SelectedIndices[0]]
                    );
            }
        }
        #endregion Methods
    }


    
}
